//
//  MHMyWalletViewController.m
//  MiaoCircle
//
//  Created by 许正荣 on 2018/7/24.
//  Copyright © 2018年 acgera. All rights reserved.
//

#import "MHMyWalletViewController.h"
#import "MHPurseHeaderView.h"
#import "MHMyWalletViewModel.h"
#import "MHPurseInfo.h"
#import "MHCarhOprtInfo.h"
#import "MHTiXianViewModel.h"
#import "MHAttentionView.h"

@interface MHMyWalletViewController ()
@property(nonatomic, strong) MHMyWalletViewModel *viewModel;
@property(nonatomic, strong) MHPurseHeaderView *purseView;
@end


@implementation MHMyWalletViewController
@dynamic viewModel;
- (void)viewDidLoad {
    [super viewDidLoad];
   UIBarButtonItem *leftBarItem = [UIBarButtonItem mh_backItemWithTitle:nil imageName: @"icon_back" target:self action:@selector(_back)];
    self.navigationItem.leftBarButtonItem = leftBarItem;
}
- (void)_back{
    [self.viewModel.services popToRootViewModelAnimated:YES];
}

-(void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    [self.navigationController.navigationBar setTitleTextAttributes:
     @{NSFontAttributeName:MHMediumFont(FontSizeFromPixel(24)),
       
       NSForegroundColorAttributeName:MHWhiteTextColor}];
}
-(void)viewWillDisappear:(BOOL)animated
{
    [super viewWillDisappear:animated];
    [self.navigationController.navigationBar setTitleTextAttributes:
     @{NSFontAttributeName:MHMediumFont(FontSizeFromPixel(24)),
       NSForegroundColorAttributeName:MHTextColor333}];
}

-(UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
    MHPurseHeaderView *purseView =  [MHPurseHeaderView mh_viewFromXib];
    @weakify(self);
    self.purseView = purseView;
    [[[self.viewModel.services.client getUserPurseInfo] mh_parsedResults] subscribeNext:^(MHPurseInfo *info) {
        @strongify(self);
        purseView.totalAmount.text = [NSString stringWithFormat:@"%.2f",info.totalIncome];
        purseView.toDayAmount.text = [NSString stringWithFormat:@"%.2f",info.todayIncome];
        purseView.tixianAmount.text = [NSString stringWithFormat:@"%.2f",info.canTxMoney];
    }];
    
    [purseView.tixianBtn bk_whenTapped:^{
        @strongify(self);
        @weakify(self);
        [[[self.viewModel.services.client getCarhOprtInfo] mh_parsedResults] subscribeNext:^(MHCarhOprtInfo *info) {
            @strongify(self);
            if (info.isHaveFollow) {
                MHTiXianViewModel *viewModel = [[MHTiXianViewModel alloc] initWithServices:self.viewModel.services params:@{MHViewModelUtilKey:info}];
                [self.viewModel.services pushViewModel:viewModel animated:YES];
                [RACObserve(viewModel.info,canTxMoney) subscribeNext:^(NSNumber *info) {
                    self.purseView.tixianAmount.text = [NSString stringWithFormat:@"%.2f",[info floatValue]];
                }];
            }else
            {
                self.zh_popupController = [zhPopupController new];
                MHAttentionView *atttionView  = [MHAttentionView mh_viewFromXib];
                atttionView.width = self.view.width * 0.8;
                atttionView.layer.cornerRadius = 6.0;
                atttionView.masksToBounds = YES;
                [atttionView.attionImageView yy_setImageWithURL:[NSURL URLWithString:info.qrCodeUrl] placeholder:MHDefaultAvatar(MHDefaultAvatarTypeSmall)];
                self.zh_popupController = [zhPopupController new];
                [self.zh_popupController presentContentView:atttionView];
                
                
                
                
                [atttionView.attentionBtn bk_whenTapped:^{
                   
                    OSMessage *msg=[[OSMessage alloc] init];
                    msg.title= @"关注百唠公众号";
                    msg.link = info.guanzhuUrl;
                    msg.desc = @"  ";
                    msg.image = [atttionView.attionImageView.image compressWithLengthLimit:31 *1024];;
                    [OpenShare shareToWeixinSession:msg Success:^(OSMessage *message) {

                    } Fail:^(OSMessage *message, NSError *error) {
                    }];
                    
                    
                }];


            }
          
        }];
    }];
   
    return purseView;
}

-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    return 211 + 30 + 63 - 31;
}

-(UIEdgeInsets)contentInset
{
    return UIEdgeInsetsMake(0, 0, 0, 0);
}

@end
